home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / e_mac / uio.c < prev   
Encoding:
C/C++ Source or Header  |  1995-09-14  |  4.7 KB  |  226 lines  |  [TEXT/CWIE]

  1. #include <Dialogs.h>
  2. #include <Events.h>
  3. #include <Files.h>
  4. #include <Stdlib.h>
  5. #include <Types.h>
  6. #include <Resources.h>
  7. #include <console.h>
  8. #include "ui.h"
  9.  
  10. #if defined( THINK_C ) || defined ( __MWERKS__ )
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <strings.h>
  14. #include "os_mac_eventchk.h"
  15. #ifdef THINK_C
  16. #include <fcntl.h>
  17. #include <stat.h>
  18. #else
  19. #include <unix.h>
  20. #endif
  21. #endif
  22.  
  23. void CenterRect(Rect *r)
  24. {
  25.     short d;
  26.     Rect *bounds;
  27.     
  28.     bounds = &qd.screenBits.bounds;
  29.     d = r->right - r->left;
  30.     r->left = bounds->left + (bounds->right - bounds->left - d) / 2;
  31.     r->right = r->left + d;
  32.     d = r->bottom - r->top;
  33.     r->top = bounds->top + (bounds->bottom - bounds->top - d) / 3;
  34.     r->bottom = r->top + d;
  35. }
  36.  
  37. void eDoDialog(int sel)
  38. {
  39.   Handle h = GetResource('ALRT', 1025);
  40.   CenterRect((Rect *) *h);
  41.   Alert(1025, nil);
  42. }
  43.  
  44. char *get_wd_name (short wd_refnum, char *postfix)
  45. {
  46. #if defined( THINK_C ) || defined( __MWERKS__ )
  47.   unsigned char buf [64];
  48. #else
  49.   char buf [64];
  50. #endif
  51.   char *temp, *result;
  52.   DirInfo cinfo;
  53.   WDPBRec wd;
  54.  
  55.   wd.ioCompletion = NULL;
  56.   wd.ioNamePtr = NULL;
  57.   wd.ioVRefNum = wd_refnum;
  58.   wd.ioWDIndex = 0;
  59.   wd.ioWDProcID = 0;
  60.   wd.ioWDVRefNum = 0;
  61.   if (PBGetWDInfo (&wd, 0) != noErr) return postfix;
  62.   cinfo.ioDrParID = wd.ioWDDirID;
  63.   result = malloc (strlen (postfix) + 1);
  64.   if (result == NULL) return postfix;
  65.   strcpy (result, postfix);
  66.   do{
  67.     cinfo.ioCompletion = NULL;
  68.     cinfo.ioNamePtr = buf;
  69.     cinfo.ioVRefNum = wd.ioWDVRefNum;
  70.     cinfo.ioFDirIndex = -1;
  71.     cinfo.ioDrDirID = cinfo.ioDrParID;
  72.     if (PBGetCatInfo ((CInfoPBPtr) &cinfo, 0) != noErr) return postfix;
  73. #ifdef THINK_C
  74.     temp = malloc (strlen (result) + buf[0] + 2);
  75. #else
  76.     p2cstr (buf);
  77.     temp = malloc (strlen (result) + strlen ((char *)buf) + 2);
  78. #endif
  79.     if (temp == NULL){
  80.       free (result);
  81.       return postfix;
  82.     }
  83. #ifdef THINK_C
  84.     sprintf (temp, ":%#s%s", buf, result);
  85. #else
  86.     sprintf (temp, ":%s%s", buf, result);
  87. #endif
  88.     free (result);
  89.     result = temp;
  90.   }while (cinfo.ioDrDirID != 2);
  91.   return result + 1;
  92. }
  93.  
  94. extern int sub_main (int argc, char *argv []);
  95.  
  96. extern unsigned char *console_title;
  97.  
  98. /* CAMLRUN  10Jan95 e
  99.    0 for a command line interface to load any Caml link'd file
  100.    1 to load image named in string resource
  101. */
  102.  
  103. #define CAMLRUN 1
  104.  
  105. #ifdef THINK_C
  106. extern WindowPeek cflush(FILE *fp);
  107. #endif
  108.  
  109. main ()
  110. {
  111.   int argc, fd;
  112.   char **argv;
  113.   WDPBRec wd;
  114.   char *args[6];
  115.   KeyMap kMap;
  116.   long len;
  117.   Str255 pstr;
  118.  
  119.   if ( GetApplLimit() > ( LMGetCurStackBase() - 40000 ) )
  120.   { SetApplLimit ( LMGetCurStackBase() - 40000 );
  121.   }
  122.  
  123.   /* 10Jan95 e -- data fork is now PPC code...
  124.   char apname[32];
  125.   unsigned char *CurApName = LMGetCurApName();
  126.   memcpy(apname,((char *)CurApName + 1),*((char *)CurApName));
  127.   apname[*((char *)CurApName)] = 0;
  128.   */
  129.  
  130. #if CAMLRUN
  131.  
  132.   wd.ioCompletion = NULL;
  133.   wd.ioNamePtr = NULL;
  134.   PBHGetVol ( &wd, 0 );
  135.  
  136.   /* arg 0 */
  137.   GetIndString(pstr, 357, 1); /* image name */
  138.   p2cstr(pstr);
  139.   args[0] = get_wd_name( wd.ioVRefNum, (char *)pstr );
  140.   fd = open( args[0], O_RDONLY | O_BINARY);
  141.   if (fd != -1){
  142.     len = lseek( fd, 0, SEEK_END );
  143.     close( fd );
  144.     GetKeys( kMap );
  145.     // fd = ((len < 12000) || (kMap[1] & 4)) ? -1 : 0; -- option key
  146.     fd = ((len < 1200) || (kMap[1] & 32768L)) ? -1 : 0;
  147.   } /* probe image, insure big enough, command key for command line */
  148.   
  149.   /* arg 1 */
  150.   args[1] = "-stdlib";
  151.  
  152.   /* arg 2 */
  153.   GetIndString(pstr, 357, 2); /* stdlib directory name */
  154.   p2cstr(pstr);
  155.   args[2] = get_wd_name( wd.ioVRefNum, (char *)pstr );
  156.  
  157.   argc = 3;
  158.  
  159.   /* optional arg 3 */
  160.   GetIndString(pstr, 357, 3);     /* options, e.g., -P */
  161.   if ( pstr[0] != 0 )
  162.   {
  163.     args[3] = malloc( pstr[0] );
  164.     if( args[3] != NULL )
  165.     { argc = 4;
  166.       p_to_c( pstr, args[3] );
  167.       /* optional arg 4 */
  168.       GetIndString(pstr, 357, 4); /* options, e.g., {full,none,etc.} */
  169.       if ( pstr[0] != 0 )
  170.       {
  171.         args[4] = malloc( pstr[0] );
  172.         if( args[4] != NULL )
  173.         { argc = 5;
  174.           p_to_c( pstr, args[4] );
  175.         }
  176.       }
  177.     }
  178.   }
  179.  
  180. #else
  181.  
  182.   fd = -1;
  183.  
  184. #endif
  185.  
  186.   GetIndString(pstr, 357, 5); /* console title */
  187.   if ( pstr[0] == 0 )
  188.     console_title = "\pMosml Console";
  189.   else
  190.     console_title = pstr;
  191.  
  192.   if ( fd != -1 )
  193.   { 
  194.     argv = args;
  195.     argv[argc] = NULL;
  196. #ifdef THINK_C
  197.     cflush(stdin);
  198. #else
  199.     puts(" ");
  200.     fflush(stdout);
  201. #endif
  202.   }
  203.   else
  204.   {
  205.     if ( 1 /* kMap[1] & 1 */  ) // shift key
  206.     {
  207.       fputs(args[0],stdout);
  208.       fputs(" ",stdout);
  209.       fputs(args[1],stdout);
  210.       fputs(" ",stdout);
  211.       fputs(args[2],stdout);
  212.       fputs(" ",stdout);
  213.       fputs(args[3],stdout);
  214.       fputs(" ",stdout);
  215.        puts(args[4]);
  216.     }
  217.     argc = ecommand( (unsigned char ***)&argv );
  218.   }
  219.  
  220.   init_timers();
  221.  
  222.   return caml_main (argc, argv);
  223. }
  224.  
  225. // end of uio.c
  226.